package PackageComplex;

public class Complex {
    private int RealPart;
    private int ImaginPart;


    public Complex() {
        RealPart = 0;
        ImaginPart = 0;
    }

    public Complex(int r, int i) {
        this.RealPart = r;
        this.ImaginPart = i;
    }

    public Complex complexAdd(Complex a) {
        return new Complex(a.RealPart + this.RealPart, a.ImaginPart + this.ImaginPart);
    }

    public Complex complexSub(Complex a) {
        return new Complex(a.RealPart - this.RealPart, a.ImaginPart - this.ImaginPart);
    }

    public Complex complexMul(Complex a) {
        return new Complex(a.RealPart * this.RealPart - a.ImaginPart * this.ImaginPart, a.ImaginPart * this.RealPart + a.RealPart * this.ImaginPart);
    }

    //复数的取模
    public double modulus() {
        return Math.sqrt(this.ImaginPart * this.ImaginPart + this.RealPart * this.RealPart);
    }

    //返回字符串
    public String ToString() {
        String s;
        if (this.RealPart != 0 && this.ImaginPart != 0) {
            if (this.ImaginPart > 0) {
                s = this.RealPart + "+" + this.ImaginPart + "i";
            } else {
                s = String.valueOf(this.RealPart) + String.valueOf(this.ImaginPart) + "i";
            }
        } else if (this.RealPart == 0) {
            s = this.ImaginPart + "i";
        } else {
            s = String.valueOf(this.RealPart);
        }
        return s;
    }

}